package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.CommentIndex;
import com.dzj.fishingcommunity.service.CommentIndexService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment-indexes")
@Tag(name = "评论索引管理", description = "评论索引管理相关的接口")
public class CommentIndexController {

    @Autowired
    private CommentIndexService commentIndexService;

    @Operation(summary = "获取所有评论索引")
    @GetMapping
    public ResponseResult<List<CommentIndex>> getAllCommentIndexes(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<CommentIndex> page = new Page<>(current, size);
        Page<CommentIndex> commentIndexPage = commentIndexService.page(page);
        return ResponseResult.success(commentIndexPage.getRecords());
    }

    @Operation(summary = "根据ID获取评论索引")
    @GetMapping("/{id}")
    public ResponseResult<CommentIndex> getCommentIndexById(@PathVariable Integer id) {
        CommentIndex commentIndex = commentIndexService.getById(id);
        return ResponseResult.success(commentIndex);
    }

    @Operation(summary = "新增评论索引")
    @PostMapping
    public ResponseResult<CommentIndex> addCommentIndex(@RequestBody CommentIndex commentIndex) {
        commentIndexService.save(commentIndex);
        return ResponseResult.success(commentIndex);
    }

    @Operation(summary = "更新评论索引")
    @PutMapping("/{id}")
    public ResponseResult<CommentIndex> updateCommentIndex(@PathVariable Integer id, @RequestBody CommentIndex commentIndex) {
        commentIndex.setId(id);
        commentIndexService.updateById(commentIndex);
        return ResponseResult.success(commentIndex);
    }

    @Operation(summary = "删除评论索引")
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteCommentIndex(@PathVariable Integer id) {
        boolean isDeleted = commentIndexService.removeById(id);
        if (!isDeleted) {
            return ResponseResult.error(400, "评论索引删除出现异常");
        }
        return ResponseResult.success(null);
    }
}